/**
 * 主界面左侧的设备树菜单
 * 根据json数据生成树
 */

CommandDeviceTree = {}
var treeObjCommand = "";
CommandDeviceTree.createTree = function (id) {
	this.updateMap = {};
	this.filterParams = {updateNode: false, filter: false}; //过滤条件
	this.treeManager = $("#" + id);
	this.load(false);
	var me = this;

}

var setting1 = {
	check: {
		enable: true,     //这里设置是否显示复选框
		chkStyle: "radio",  //单选框
		radioType: "all",
		chkboxType: {"Y": "", "N": ""}      //设置复选框是否与 父/子 级相关联
	},
	data: {
		simpleData: {
			enable: true
		}, key: {
			title: "simNo"
		}
	}, callback: {
		//onClick: onClicko,
	}, view: {
		showTitle: false,
		fontCss: setHighlight // 高亮一定要设置，setHighlight是自定义方法
	}
};


CommandDeviceTree.load = function (isUpdate) {
	if (isUpdate == false)
		this.updateMap = {};
	var me = this;
	var treeManager = this.treeManager;
	var url = path + "/deviceBind/getDeviceTree";
	//var params = {updateNode:update};
	this.filterParams.updateNode = isUpdate;
	var filter = false;
	$.ajax({
		type: "POST",
		url: url,
		data: this.filterParams,
		error: function () {
			//alert("网络连接错误，无法读取数据!");
			//Utility.done();
		},
		success: function (data) {
			//登录超时
			if (data.logoutFlag) {
				window.top.location = path + "/login";
				return false;
			}
			//console.info(eval(data.TreeDataIndex));
			if (isUpdate == false) {
				me.initializeCallback(data); //填充到树中，生成设备数
			} else {
				var nodeData = data.UpdateTreeDataIndex;
				var zTree = $.fn.zTree.getZTreeObj("deviceTreeCommand");
				for (var i = 0; i < nodeData.length; i++) {
					var node = zTree.getNodeByParam("id", nodeData[i].id, null);
					node.name = nodeData[i].name;
					node.icon = nodeData[i].icon;
					zTree.updateNode(node);
				}
				//zTree.reAsyncChildNodes(null, "refresh");
			}
			//$("#spanOnlineUserNum").html(onlineInfo.onlineUserNum);
			//me.manager.setData(treeData);
			var treeObjCommand = $.fn.zTree.getZTreeObj("deviceTreeCommand");
			var nodes = treeObjCommand.getNodes();
			if (nodes.length > 0) {
				for (var i = 0; i < nodes.length; i++) {
					treeObjCommand.expandNode(nodes[i], true, false, false);
				}
			}
		}
	});

}


/**
 * 用于在获取数据后，填充设备树
 */
CommandDeviceTree.initializeCallback = function (data) {
	var treeRightMenu = this.treeManagerRightMenu;

	treeObjCommand = $.fn.zTree.init($("#deviceTreeCommand"), setting1, data.TreeDataIndex);
	var zTree = $.fn.zTree.getZTreeObj("deviceTreeCommand"),        // 获取ztree对象
		node = zTree.getNodes(),      //获取根节点
		nodes = zTree.transformToArray(node);    //遍历所有节点
	if (nodes.length > 0) {
		for (var i = 0; i < nodes.length; i++) {
			if (nodes[i].id.indexOf("dep") == 0) {        //判断需要显示复选框的条件
				nodes[i].nocheck = true;               //nocheck为true表示没有选择框
				zTree.updateNode(nodes[i]);         //刷新
			}
		}
	}
}

CommandDeviceTree.getSelectNode = function () {
	var treeObjCommand = $.fn.zTree.getZTreeObj("deviceTreeCommand");
	var sNodes = treeObjCommand.getSelectedNodes();
	var node;
	if (sNodes.length > 0) {
		node = sNodes[0];
	}

	return node;
}


// 有输入后定时执行一次，如果上次的输入还没有被执行，那么就取消上一次的执行
var timeoutId = null;
CommandDeviceTree.Treefilter = function (filterType, value) {
	if (value == null || $.trim(value) == "") {
		$.iMessager.alert('提示', '请输入查询内容', 'messager-warning');
	}
	if (timeoutId) {
		clearTimeout(timeoutId);
	}
	timeoutId = setTimeout(function () {
		searchNodeCommand(filterType, value);
	}, 500);

}

//精确搜索
function searchNodeCommand(filterType, value) {
	if (value == "") return;

	//搜索设备号
	if (filterType == 0) {
		// 获取与value匹配的节点，使用的ztreeAPI getNodesByParam
		var nodes = treeObjCommand.getNodesByParam("simNo", $.trim(value));
		if (nodes.length > 0) {
			for (var i = 0; i < nodes.length; i++) {
				if (nodes[i].id.indexOf("v") == 0) {
					treeObjCommand.selectNode(nodes[i]);
					break;
				}
			}
		} else {
			treeObjCommand.cancelSelectedNode();
		}
	}
	//搜索显示名称
	if (filterType == 1) {
		// 获取与value匹配的节点，使用的ztreeAPI getNodesByParam
		var nodes = treeObjCommand.getNodesByParamFuzzy("name", $.trim(value));
		if (nodes.length > 0) {
			for (var i = 0; i < nodes.length; i++) {
				if (nodes[i].id.indexOf("v") == 0) {
					treeObjCommand.selectNode(nodes[i]);
					break;
				}
			}
		} else {
			treeObjCommand.cancelSelectedNode();
		}
	}
	//搜索分组
	if (filterType == 2) {
		// 获取与value匹配的节点，使用的ztreeAPI getNodesByParamFuzzy
		var nodes = treeObjCommand.getNodesByParamFuzzy("name", $.trim(value));
		if (nodes.length > 0) {
			for (var i = 0; i < nodes.length; i++) {
				if (nodes[i].id.indexOf("dep") == 0) {
					treeObjCommand.selectNode(nodes[i]);
					break;
				}
			}
		} else {
			treeObjCommand.cancelSelectedNode();
		}
	}

}


// 设置高亮字体
function setHighlight(treeId, treeNode) {
	return (!!treeNode.highlight) ? {color: "#A60000", "font-weight": "bold"} : {
		color: "#333",
		"font-weight": "normal"
	};
}